package model;

import java.sql.ResultSet;
import java.util.ArrayList;

import database.ProductOperations;
public class ProductCollection 
{
	private ArrayList<Product> prodlist;
	private ResultSet rst;
	private ProductOperations po;
	public ProductCollection(ProductOperations p)
	{
		po=p;
		prodlist=new ArrayList<Product>();
	}
	
	
	
	public Product getProduct(int i) 
	{
		return prodlist.get(i);
	}

	public int getNumProducts() 
	{
		return prodlist.size();
	}
	
	public boolean removeProduct(int id) 
	{
		boolean ans=false;
		for (int i = 0; i < prodlist.size(); i++) 
		{
			if (id==prodlist.get(i).getId()) 
			{
				prodlist.remove(i);
				po.deleteProduct(id);
				ans= true;
			}
		}
		return ans;
	}
	
	public void addProduct()
	{
		rst = po.getLastRow();
		try 
		{
			Product p = new Product(rst.getInt(1), rst.getString(2),
			rst.getString(3), rst.getString(4), rst.getDouble(5));
			prodlist.add(p);
			//System.out.println(rst.getInt(1)+ rst.getString(2)+rst.getString(3)+ rst.getString(4)+ rst.getDouble(5));
		} 
		catch (Exception e) 
		{
			System.out.println(e);
		}
	}
	public void updatePrice(int id,double price) 
	{
		for (int i = 0; i < prodlist.size(); i++) 
		{
			if (id==prodlist.get(i).getId())
			{
				prodlist.get(i).setPrice(price);;
		
			}
		}	
		//po.updateProductPrice(id,price);
	}
	
	
	public int findProduct(int id) 
	{
		int index = -1;
		for (int i = 0; i < prodlist.size(); i++) 
		{
			if (id==prodlist.get(i).getId()) 
			{
				index = i;
			}
		}
		return index;
	}
}


